package com.ymatou.iapi.trading.operate.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.OrderVo;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.req.ChangeEcommerceFlagReq;
import com.ymatou.iapi.trading.operate.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.trading.operate.service.ChangeEcommerceFlagCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;

/**
 * 贝海实际电商件通知接口
 */
@TestSuite
public class Ts_ChangeEcommerceFlag {
	private static ChangeEcommerceFlagReq changeEcommerceFlagReq;
	private static ChangeEcommerceFlagCall changeEcommerceFlagCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("贝海实际电商件通知接口");
	}

	@Before
	public void caseUp() {
		changeEcommerceFlagReq = new ChangeEcommerceFlagReq();
		changeEcommerceFlagCall = new ChangeEcommerceFlagCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(changeEcommerceFlagCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 一个订单下面多个商品, 老买手，新用户
	 * @throws Exception 
	 * 
	 */
	private PlaceOrderResp placeOrder(String receiveName,
			boolean isEcommerceFlag) throws Exception {

		PlaceOrderReq placeorderBean = PlaceOrderService.getDefaultOrderReq();

		OrderVo order = placeorderBean.getOrderGroups().get(0).getOrders()
				.get(0);

		order.setReceiveName(receiveName);

		order.setEcommerceFlag(isEcommerceFlag);

		return PlaceOrderService.callPlaceOrderService(placeorderBean);

	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeEcommerceFlag_001() {
		Logger.start(true, "订单预测个人件,贝海通知为电商件");
		try {
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = placeOrder("李四四", false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			Logger.comment("前置数据准备结束");
			changeEcommerceFlagReq.setOrderId(orderId);
			changeEcommerceFlagReq.setEcommerceFlag(1);
			changeEcommerceFlagReq.setAppId("autotest");
			changeEcommerceFlagReq.setBillCode("autotest111");

			changeEcommerceFlagCall.setData(changeEcommerceFlagReq);
			changeEcommerceFlagCall.callService();

			Logger.verifyEquals("ok", changeEcommerceFlagCall.getReturnData(),
					"验证接口返回报文");

			//数据库验证

			DefaultWapper database = new DefaultWapper();

			// 订单扩展表
			List<Map> ymtOrdersExt = database.selectOrderExtByOderId(orderId);

			Logger.verifyEquals(false, ymtOrdersExt.get(0)
					.get("bEcommerceFlag"), "验证是否为电商件");

			Logger.verifyEquals(1,
					(Integer) ymtOrdersExt.get(0).get("iXloboEcommerceFlag"),
					"验证贝海通知是否为电商件");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeEcommerceFlag_002() {
		Logger.start(true, "订单预测个电商件,贝海通知为个人件");
		try {
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = placeOrder("李四四", true);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			Logger.comment("前置数据准备结束");
			changeEcommerceFlagReq.setOrderId(orderId);
			changeEcommerceFlagReq.setEcommerceFlag(0);
			changeEcommerceFlagReq.setAppId("autotest");
			changeEcommerceFlagReq.setBillCode("autotest111");

			changeEcommerceFlagCall.setData(changeEcommerceFlagReq);
			changeEcommerceFlagCall.callService();

			Logger.verifyEquals("ok", changeEcommerceFlagCall.getReturnData(),
					"验证接口返回报文");

			//数据库验证

			DefaultWapper database = new DefaultWapper();

			// 订单扩展表
			List<Map> ymtOrdersExt = database.selectOrderExtByOderId(orderId);

			Logger.verifyEquals(true,
					ymtOrdersExt.get(0).get("bEcommerceFlag"), "验证是否为电商件");

			Logger.verifyEquals(changeEcommerceFlagReq.getEcommerceFlag(),
					(Integer) ymtOrdersExt.get(0).get("iXloboEcommerceFlag"),
					"验证贝海通知是否为电商件");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeEcommerceFlag_003() {
		Logger.start(true, "订单预测个电商件,贝海通知为不存在的类型件3");
		try {
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = placeOrder("李四四", true);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			Logger.comment("前置数据准备结束");
			changeEcommerceFlagReq.setOrderId(orderId);
			changeEcommerceFlagReq.setEcommerceFlag(3);
			changeEcommerceFlagReq.setAppId("autotest");
			changeEcommerceFlagReq.setBillCode("autotest111");

			changeEcommerceFlagCall.setData(changeEcommerceFlagReq);
			changeEcommerceFlagCall.callService();

			Logger.verifyEquals("ok", changeEcommerceFlagCall.getReturnData(),
					"验证接口返回报文");

			//数据库验证

			DefaultWapper database = new DefaultWapper();

			// 订单扩展表
			List<Map> ymtOrdersExt = database.selectOrderExtByOderId(orderId);

			Logger.verifyEquals(true,
					ymtOrdersExt.get(0).get("bEcommerceFlag"), "验证是否为电商件");

			Logger.verifyEquals(3,
					(Integer) ymtOrdersExt.get(0).get("iXloboEcommerceFlag"),
					"验证贝海通知是否为电商件");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeEcommerceFlag_004() {
		Logger.start(true, "订单预测个人件,贝海通知为个人件");
		try {
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = placeOrder("李四四", false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			Logger.comment("前置数据准备结束");
			changeEcommerceFlagReq.setOrderId(orderId);
			changeEcommerceFlagReq.setEcommerceFlag(0);
			changeEcommerceFlagReq.setAppId("autotest");
			changeEcommerceFlagReq.setBillCode("autotest111");

			changeEcommerceFlagCall.setData(changeEcommerceFlagReq);
			changeEcommerceFlagCall.callService();

			Logger.verifyEquals("ok", changeEcommerceFlagCall.getReturnData(),
					"验证接口返回报文");

			//数据库验证

			DefaultWapper database = new DefaultWapper();

			// 订单扩展表
			List<Map> ymtOrdersExt = database.selectOrderExtByOderId(orderId);

			Logger.verifyEquals(false, ymtOrdersExt.get(0)
					.get("bEcommerceFlag"), "验证是否为电商件");

			Logger.verifyEquals(changeEcommerceFlagReq.getEcommerceFlag(),
					(Integer) ymtOrdersExt.get(0).get("iXloboEcommerceFlag"),
					"验证贝海通知是否为电商件");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}
